﻿create procedure [dbo].[usp_LogEntryInsert]
	@PublicationId uniqueidentifier
	, @AppDomainId int
	, @AssemblyId int
	, @MachineId int
	, @ProcessId int
	, @ThreadId int
	, @WorkItemId int
	, @LogEntry nvarchar(max) 
	, @Priority int
	, @Type int
	, @SerializedIIdentityXml xml
	, @IIdentityName nvarchar(255) 
	, @IIdentityIsAuthenticated bit
	, @IIdentityAuthenticationType nvarchar(255) 
as

begin try
	
	begin transaction
	
		if not exists(select * from [dbo].[LogEntry] where PublicationId = @PublicationId and
		                                                   AppDomainId = @AppDomainId and
		                                                   AssemblyId = @AssemblyId and
		                                                   ([MachineId] = @MachineId or ([MachineId] is null and @MachineId is null)) and
													       ([ProcessId] = @ProcessId or ([ProcessId] is null and @ProcessId is null)) and
		                                                   ThreadId = @ThreadId and
		                                                   ([WorkItemId] = @WorkItemId or ([WorkItemId] is null and @WorkItemId is null)) and
		                                                   LogEntry = @LogEntry and
		                                                   Priority = @Priority and
		                                                   Type = @Type and
		                                                   [dbo].[CompareXml]([SerializedIIdentityXml], @SerializedIIdentityXml) = 0 and
		                                                   IIdentityName = @IIdentityName and
		                                                   IIdentityIsAuthenticated = @IIdentityIsAuthenticated and
		                                                   IIdentityAuthenticationType = @IIdentityAuthenticationType)
			begin
										                              
				insert [dbo].[LogEntry]
				(
					PublicationId
					, AppDomainId
					, AssemblyId
					, MachineId
					, ProcessId
					, ThreadId
					, WorkItemId
					, LogEntry 
					, Priority
					, Type
					, SerializedIIdentityXml
					, IIdentityName
					, IIdentityIsAuthenticated
					, IIdentityAuthenticationType
				)
				values
				(
					@PublicationId
					, @AppDomainId
					, @AssemblyId
					, @MachineId
					, @ProcessId
					, @ThreadId
					, @WorkItemId
					, @LogEntry 
					, @Priority
					, @Type
					, @SerializedIIdentityXml
					, @IIdentityName
					, @IIdentityIsAuthenticated
					, @IIdentityAuthenticationType
				)
				
				select LogEntryId, ConcurrencyId, PublicationId, Created, Updated from [dbo].[LogEntry] where @@rowcount > 0 and AssemblyId = scope_identity()
			
			end
			
		else
			
			begin
			
				select LogEntryId, ConcurrencyId, PublicationId, Created, Updated from [dbo].[LogEntry] where PublicationId = @PublicationId and
																										      AppDomainId = @AppDomainId and
																										      AssemblyId = @AssemblyId and
																										      ([MachineId] = @MachineId or ([MachineId] is null and @MachineId is null)) and
															                                                  ([ProcessId] = @ProcessId or ([ProcessId] is null and @ProcessId is null)) and
																										      ThreadId = @ThreadId and
																										      ([WorkItemId] = @WorkItemId or ([WorkItemId] is null and @WorkItemId is null)) and
																										      LogEntry = @LogEntry and
																										      Priority = @Priority and
																										      Type = @Type and
																										      [dbo].[CompareXml]([SerializedIIdentityXml], @SerializedIIdentityXml) = 0 and
																										      IIdentityName = @IIdentityName and
																										      IIdentityIsAuthenticated = @IIdentityIsAuthenticated and
																										      IIdentityAuthenticationType = @IIdentityAuthenticationType
			end
	
	commit transaction

end try
	
begin catch
	if @@trancount > 0
	begin
		rollback transaction;
	end
	execute [dbo].[usp_SqlErrorInsert]
end catch


